package com.swxy.config;

import com.swxy.filter.LoginGatewayFilter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 易
 */

@Configuration
public class GatewayConfig {

    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder, LoginGatewayFilter loginFilter) {
        return builder.routes()
                // 拦截所有 /api/ 开头的请求
                .route("api_route", r -> r.path("/user-serv/api/**")
                        .filters(f -> f.stripPrefix(1) //去掉第一层路径
                                .filter(loginFilter)) // 应用自定义拦截器
                        .uri("lb://user-service")) // 路由到目标服务
                .build();
    }
}
